package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    //注入游记


    @Autowired
    private ITravelService travelService;


    //前台发送注册信息给后台查询验证返回数据
    @PostMapping
    public JsonResult register(User user) {
        JsonResult jsonResult = new JsonResult();

        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public JsonResult update(User user) {
        JsonResult jsonResult = new JsonResult();
        userService.updateByPrimaryKey(user);
        jsonResult.setResult(user);
        return jsonResult;
    }

    /**
     * 查询游记
     * @return pageinfo
     */
    @GetMapping("/{authorId}/travels")
    public PageInfo queryTravel(TravelQueryObject qo){

        qo.setOrderBy("lastUpdateTime DESC");
        return travelService.queryTravel(qo);
    }


}
